﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GameSPK.Model;
using System.Windows.Controls;
using GameSPK.Helpers;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameSPK.DAO;
using System.Windows;


namespace GameSPK.Start
{
    class frmOChuStart: frmGenerel
    {
        private static frmOChuStart instance;
        private static object syncLock = new object();
        
        public static frmOChuStart getInstance()
        {
            if (instance == null)
            {
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        instance = new frmOChuStart();
                        
                    }
                }
            }

            return instance;
        }
        private frmOChuStart()
            : base()
        {

            this.Closed += new EventHandler(frmOChuStart_Closed);
            this.Loaded += new System.Windows.RoutedEventHandler(frmOChuStart_Loaded);
            AddHandler(Keyboard.KeyDownEvent, (System.Windows.Input.KeyEventHandler)frmOChuStart_KeyDown);
            
        }
        System.Windows.Forms.Timer timerClock;
        
        void frmOChuStart_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            filename = "section3layout.xml";
            crossWordLayout = CrossWordHelp.getCrossWordLayoutFromXML(filename);
            //cauhoi = CauHoiDAO.getCauHoiTrinhChieu(MainWindow.getCuocThiChon(), 3, 1);

            timerClock = new System.Windows.Forms.Timer();
            timerClock.Tick += new EventHandler(timerClock_Tick);
            timerClock.Interval = 1000;
            timerClock.Enabled = false;
            Generate();
        }

        void timerClock_Tick(object sender, EventArgs e)
        {
            timetick--;
            if (timetick >= 0)
            {
                lblClock.Content = timetick;
            }
            else
            {
                timerClock.Enabled = false;
            }
        }
        string filename;
        LabelLayout lblLayoutClock;
        int timetick;
        Label lblClock;
        CauHoi cauhoi;
        Label lblContent;
        TextBlock tbcontent;
        CrossWordLayOut crossWordLayout;
        CrossWord crossWord;
        List<HangNgangStart> hangngang;
        void Generate()
        {
            //button
            Button btnHome;
            Button btnAnswer;
            Control cbtnHome, cbtnAnswer;
            btnHome = new Button();
            cbtnHome = (Control)btnHome;
            LabelLayout lableXML;
            lableXML = Helper.getButtonFromXML(filename, "HomeButton");
            AssignControl.assignControlLayout(ref cbtnHome, lableXML, "btnHome");
            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                   new BitmapImage(new Uri(ConfigXMLDAO.getHomeButtonUrl(), UriKind.RelativeOrAbsolute));
            btnHome.Background = myBrush;
            btnHome.Click += new System.Windows.RoutedEventHandler(btnHome_Click);
            gridMain.Children.Add(btnHome);

            btnAnswer = new Button();
            cbtnAnswer = (Control)btnAnswer;

            lableXML = Helper.getButtonFromXML(filename, "DapAnButton");
            AssignControl.assignControlLayout(ref cbtnAnswer, lableXML, "btnAnswer");
            ImageBrush myBrush2 = new ImageBrush();
            myBrush2.ImageSource =
                   new BitmapImage(new Uri(ConfigXMLDAO.getAnswerButtonUrl(), UriKind.RelativeOrAbsolute));
            btnAnswer.Background = myBrush2;
            btnAnswer.Click+=new RoutedEventHandler(btnAnswer_Click);
            btnAnswer.Click += new System.Windows.RoutedEventHandler(btnAnswer_Click);
            gridMain.Children.Add(btnAnswer);
            //question
            lblContent = new Label();
            Control clblContent = (Control)lblContent;
            lableXML = Helper.getLabelFromXML(filename, "Question");
            AssignControl.assignControlLayout(ref clblContent, lableXML, "lblQuestion");

            lblContent.FontSize = lableXML.FontSize;
            fontsizecontent = lableXML.FontSize;
            lblContent.FontWeight = System.Windows.FontWeights.Normal;
            lblContent.Foreground = lableXML.Foreground;
            lblContent.Background = lableXML.Background;
            tbcontent = new TextBlock();
            tbcontent.TextWrapping = TextWrapping.Wrap;
            tbcontent.FontSize = lableXML.FontSize;
            lblContent.Visibility = System.Windows.Visibility.Hidden;
            lblContent.Content = tbcontent;
            lblContent.MouseDoubleClick += new MouseButtonEventHandler(lblContent_MouseDoubleClick);
            gridMain.Children.Add(lblContent);
            //clock
            //clock
            lblClock = new Label();
            Control clblClock = (Control)lblClock;
            lblLayoutClock = CrossWordHelp.getClockCrossWordFromXML(filename);
            AssignControl.assignControlLayout(ref clblClock, lblLayoutClock, "lblClock");
            lblClock.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
            lblClock.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            lblClock.FontSize = lblLayoutClock.FontSize;
            lblClock.FontWeight = System.Windows.FontWeights.Bold;
            lblClock.Style = (Style)this.FindResource("PulseLabel");
            lblClock.Foreground = lblLayoutClock.Foreground;
            lblClock.Content = lblLayoutClock.iTime1;
            DrawingBrush myDrawingBrush = new DrawingBrush();
            GeometryDrawing myGeometryDrawing = new GeometryDrawing();
            myGeometryDrawing.Brush = lblLayoutClock.Background;

            myGeometryDrawing.Pen = new Pen(Brushes.Red, 1);
            GeometryGroup ellipses = new GeometryGroup();
            ellipses.Children.Add(new EllipseGeometry(new Point(25, 50), 50, 50));
            myGeometryDrawing.Geometry = ellipses;
            myDrawingBrush.Drawing = myGeometryDrawing;
            lblClock.Background = myDrawingBrush;
            lblClock.Visibility = System.Windows.Visibility.Hidden;
            //lblClock.MouseDoubleClick += new MouseButtonEventHandler(lblClock_MouseDoubleClick);
            timetick = lblLayoutClock.iTime1;
            //timerClock.Enabled = true;
            //timerClock.Start();
            gridMain.Children.Add(lblClock);

            //o chu
            crossWordLayout = CrossWordHelp.getCrossWordLayoutFromXML(filename);
            cauhoi = CauHoiDAO.getCauHoiTrinhChieu(MainWindow.getCuocThiChon(), 3, 1);
            if (cauhoi == null)
                return;
            crossWord = CrossWordHelp.getCrossWord(cauhoi.CauHoi1);

            generateHangNgang();

            //lblContent.Visibility = System.Windows.Visibility.Visible;
            //tbcontent.Text = hangngang[0].CauHoi;
        }
        bool isdapan = false;
        void lblContent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //hien thi goi y o chu chinh va thoi gian

            isdapan = true;
            tbcontent.Text = crossWord.chude.Trim();
            timetick = lblLayoutClock.iTime2;
            timerClock.Enabled = true;
        }
        List<List<Label>> lstLableHangNgang;
        double leftHangngan, topHangngang;
        double left, top;
        void generateHangNgang()
        {
            lstLableHangNgang = new List<List<Label>>();
            hangngang = new List<HangNgangStart>();
            leftHangngan = crossWordLayout.Left;
            topHangngang = crossWordLayout.Top;
            for (int i = 0; i < crossWord.sohangngang; i++)
            {
                List<Label> lstHangNgang = new List<Label>();
                HangNgangStart hangngangi = CrossWordHelp.getHangNgangi(cauhoi.CauHoi1, i);
                hangngang.Add(hangngangi);
                left = leftHangngan;
                top = topHangngang;
                for (int j = 0; j < hangngangi.soKyTu; j++)
                {
                    Label lblochu = new Label();
                    lblochu.Name = "hangngang" + i;
                    lblochu.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    lblochu.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    lblochu.Margin = new Thickness(left, top, 0, 0);
                    lblochu.FontSize = crossWordLayout.FontSize;
                    lblochu.FontWeight = System.Windows.FontWeights.Bold;
                    lblochu.Visibility = System.Windows.Visibility.Visible;
                    lblochu.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                    lblochu.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                    lblochu.Width = crossWordLayout.Width;
                    lblochu.Height = crossWordLayout.Height;
                    lblochu.Background = crossWordLayout.BackgroundStart;
                    lblochu.MouseDoubleClick += new MouseButtonEventHandler(lblochu_MouseDoubleClick);
                    bool flag = true;
                    foreach (int l in hangngangi.vitriKeyChar)
                    {
                        if (j == l)
                        {
                            flag = false;
                            lblochu.Foreground = crossWordLayout.ColorKey;
                            break;
                        }
                    }
                    if (flag)
                        lblochu.Foreground = crossWordLayout.ColorNotKey;
                    
                    lstHangNgang.Add(lblochu);
                    gridMain.Children.Add(lblochu);
                    left += crossWordLayout.Width+1;


                }
                lstLableHangNgang.Add(lstHangNgang);
                topHangngang += crossWordLayout.Height + 10;
            }
        }
        bool isstart = false;
        bool isluachon = false;
        int hangngangthu;
        double fontsizecontent;
        void lblochu_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //
            if (!isstart)
            {
                Label lblhang = (Label)sender;
                lblContent.Content = tbcontent;
                lblContent.FontWeight = System.Windows.FontWeights.Normal;
                lblContent.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
                lblContent.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
                lblContent.FontSize = fontsizecontent;
                hangngangthu = int.Parse(lblhang.Name.Replace("hangngang", ""));
                timetick = lblLayoutClock.iTime1;
                //chuyen sang mau lua chon;
                lblContent.Visibility = System.Windows.Visibility.Visible;
                tbcontent.Text = hangngang[hangngangthu].CauHoi.Trim();
                lblClock.Visibility = System.Windows.Visibility.Visible;
                timerClock.Enabled = true;
                foreach (Label lableinlist in lstLableHangNgang[hangngangthu])
                {
                    lableinlist.Background = crossWordLayout.BackgroundSelected;
                }
                isluachon = true;
            }
        }
        void frmOChuStart_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (isluachon)
            {
                if (e.Key == Key.Home)
                {
                    isstart = false;
                    isluachon = false;
                    int j = 0;
                    char [] kytu = hangngang[hangngangthu].OHangNgang.ToArray();
                    timerClock.Enabled = false;
                    foreach (Label lableinlist in lstLableHangNgang[hangngangthu])
                    {
                        lableinlist.Background = crossWordLayout.BackgroundOpen;
                        lableinlist.Content = kytu[j++];
                    }

                }
                else if (e.Key == Key.Escape)
                {
                    isstart = false;
                    isluachon = false;
                    timerClock.Enabled = false;
                    //char[] kytu = hangngang[hangngangthu].OHangNgang.ToArray();
                    foreach (Label lableinlist in lstLableHangNgang[hangngangthu])
                    {
                        lableinlist.Background = crossWordLayout.BackgroundClose;
                        
                    }

                }
            }
        }
        
        void btnAnswer_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (isdapan)
            {
                lblContent.FontSize = crossWordLayout.FontSize;
                lblContent.Foreground = crossWordLayout.ColorKey;
                lblContent.Background = crossWordLayout.BackgroundOpen;
                lblContent.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                lblContent.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                lblContent.FontWeight = System.Windows.FontWeights.Bold;
                lblContent.Content = crossWord.dapan.ToUpper();
                timerClock.Enabled = false;
                isdapan = false;
            }
        }

        void btnHome_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Close();
        }
        void frmOChuStart_Closed(object sender, EventArgs e)
        {
            instance = null;
        }
        
    }
}
